// export * from './common'
// export * from './account'

import axios from 'axios'
import Toast from 'react-native-root-toast'
import {ENVS} from '../config'


axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'; //请求头
axios.defaults.headers.Accept = 'application/json'; //请求头
axios.defaults.baseURL = '';
axios.defaults.withCredentials = true;

axios.interceptors.request.use((config) => {
    return config;
}, (error) => {
    Toast.show('参数错误',{
        duration: Toast.durations.SHORT,
        position: Toast.positions.CENTER
    })
    return Promise.reject(error);
})

axios.interceptors.response.use((res)=>{
    if(!res.data){
        return Promise.reject(res);
    }
    if(res.data.code === 'fail'){
        Toast.show(res.data.message, {position: Toast.positions.CENTER})
    }
    return res.data;
}, (error)=> {
    Toast.show('网络错误',{
        duration: Toast.durations.SHORT,
        position: Toast.positions.CENTER
    })
    return Promise.reject(error)
})

export default {
    account: {
        login: function (params){
            // return fetch(ENVS['production'].base_url + 'api/v1/login.htm',{
            //     body: JSON.stringify(params),
            //     method: 'POST'
            // })
            var _params = JSON.stringify(params);
            return axios.post(ENVS['production'].base_url + 'api/v1/login.htm', _params);
        }
    }
}
